﻿using Android.App;
using Android.Views;
using Android.Widget;
using System.Collections.Generic;

namespace WSYmi.ConnectAct
{
    /// <summary>
    /// 蓝牙设备列表定制适配器
    /// </summary>
    public class ConnectAdapter : BaseAdapter<MyDevice>
    {
        private readonly List<MyDevice> Items;

        private readonly Activity Context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">ListView所在Activity</param>
        /// <param name="items">要在列表中显示的项目</param>
        public ConnectAdapter(Activity context, List<MyDevice> items) : base()
        {
            Context = context;
            Items = items;
        }

        public override MyDevice this[int position]
        {
            get { return Items[position]; }
        }

        public override int Count
        {
            get { return Items.Count; }
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = Items[position];
            View view = convertView;
            view ??= Context.LayoutInflater.Inflate(Resource.Layout.Connect_item, null);

            //项目属性赋予控件.Text
            view.FindViewById<TextView>(Resource.Id.TvName).Text = item.Name;
            view.FindViewById<TextView>(Resource.Id.TvRiss).Text = item.Rssi;
            return view;
        }
    }
}